package com.gaohe.web.controller.scp;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.gaohe.common.annotation.Log;
import com.gaohe.common.core.controller.BaseController;
import com.gaohe.common.core.domain.AjaxResult;
import com.gaohe.common.enums.BusinessType;
import com.gaohe.scp.domain.CsCollect;
import com.gaohe.scp.service.ICsCollectService;
import com.gaohe.common.utils.poi.ExcelUtil;
import com.gaohe.common.core.page.TableDataInfo;

/**
 * 用户收藏Controller
 *
 * @author liuxiangyu
 * @date 2025-03-25
 */
@RestController
@RequestMapping("/scp/collect")
public class CsCollectController extends BaseController
{
    @Autowired
    private ICsCollectService csCollectService;


    // 通过userId和carId查用户是否收藏了这个表------牛相善
//    @PreAuthorize("@ss.hasPermi('scp:visit:query')")
    @GetMapping(value = "/user")
    public int selectByUserId(CsCollect csCollect)
    {
        Long userId = csCollect.getUserId();
        Long carId = csCollect.getCarId();
        return (csCollectService.selectByUserId(userId,carId));
    }

    // 通过userId和carId给用户取消收藏这辆车------牛相善
//    @PreAuthorize("@ss.hasPermi('scp:collect:remove')")
    @Log(title = "用户收藏", businessType = BusinessType.DELETE)
    @GetMapping("/car/user")
    public AjaxResult removeByCarId(CsCollect csCollect)
    {
        System.out.println(csCollect);
        Long carId = csCollect.getCarId();
        Long userId = csCollect.getUserId();
        return toAjax(csCollectService.deleteCsCollectByCarId(userId,carId));
    }

    // 新增用户收藏----牛相善
//    @PreAuthorize("@ss.hasPermi('scp:collect:add')")
    @Log(title = "用户收藏", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CsCollect csCollect)
    {
        return toAjax(csCollectService.insertCsCollect(csCollect));
    }



    /**
     * 查询用户收藏列表
     */
//    @PreAuthorize("@ss.hasPermi('scp:collect:list')")
    @GetMapping("/list")
    public TableDataInfo list(CsCollect csCollect)
    {
        startPage();
        List<CsCollect> list = csCollectService.selectCsCollectList(csCollect);
        return getDataTable(list);
    }

    /**
     * 导出用户收藏列表
     */
//    @PreAuthorize("@ss.hasPermi('scp:collect:export')")
    @Log(title = "用户收藏", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CsCollect csCollect)
    {
        List<CsCollect> list = csCollectService.selectCsCollectList(csCollect);
        ExcelUtil<CsCollect> util = new ExcelUtil<CsCollect>(CsCollect.class);
        util.exportExcel(response, list, "用户收藏数据");
    }

    /**
     * 获取用户收藏详细信息
     */
//    @PreAuthorize("@ss.hasPermi('scp:collect:query')")
    @GetMapping(value = "/{collectId}")
    public AjaxResult getInfo(@PathVariable("collectId") Long collectId)
    {
        return success(csCollectService.selectCsCollectByCollectId(collectId));
    }



    /**
     * 修改用户收藏
     */
//    @PreAuthorize("@ss.hasPermi('scp:collect:edit')")
    @Log(title = "用户收藏", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CsCollect csCollect)
    {
        return toAjax(csCollectService.updateCsCollect(csCollect));
    }

    /**
     * 删除用户收藏
     */
//    @PreAuthorize("@ss.hasPermi('scp:collect:remove')")
    @Log(title = "用户收藏", businessType = BusinessType.DELETE)
	@DeleteMapping("/{collectIds}")
    public AjaxResult remove(@PathVariable Long[] collectIds)
    {
        return toAjax(csCollectService.deleteCsCollectByCollectIds(collectIds));
    }
}
